package com.wego.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wego.bean.IdName;
import com.wego.common.base.impl.BaseServiceImpl;
import com.wego.common.bean.PageBean;
import com.wego.entity.domain.City;
import com.wego.entity.domain.Country;
import com.wego.entity.domain.Province;
import com.wego.entity.query.CountryQuery;
import com.wego.mapper.CityMapper;
import com.wego.mapper.ProvinceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wego.mapper.CountryMapper;
import com.wego.service.CountryService;

import java.util.ArrayList;
import java.util.List;

@Service
public class CountryServiceImpl extends BaseServiceImpl<Country> implements CountryService{

    @Autowired
    private CountryMapper countryMapper;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private ProvinceMapper provinceMapper;

    @Override
    public PageBean<Country> selectPage(CountryQuery CountryQuery) {
        //设置分页信息
        Page<Country> page = PageHelper.startPage(CountryQuery.getPageNum(), CountryQuery.getPageSize());
        //查询数据
        countryMapper.selectList(CountryQuery);

        //将MyBatis提供的Page对象转换成我们自己的PageBean对象
        PageBean<Country> pageBean = new PageBean<>(page);

        return pageBean;
    }

    @Override
    public List<IdName> selectProvinceCityCountryByCountryId(Long countryId) {
        List<IdName> list = new ArrayList<>();

        final Country country = countryMapper.selectByPrimaryKey(countryId);
        list.add(new IdName(countryId, country.getName()));

        final City city = cityMapper.selectByPrimaryKey(country.getCityId());
        list.add(new IdName(city.getId(), city.getName()));

        final Province province = provinceMapper.selectByPrimaryKey(city.getProvinceId());
        list.add(new IdName(province.getId(), province.getName()));

        return list;
    }

    @Override
    public List<Country> selectByCityId(Long cityId) {
        final List<Country> countryList = countryMapper.selectList(CountryQuery.builder()
                .cityId(cityId)
                .build());
        return countryList;
    }

}
